package com.fy56.platform.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.BaseController;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.model.MiniProgram;
import com.fy56.platform.logistics.service.web.IMiniProgramService;
import com.fy56.platform.logistics.utils.LogUtils;
import com.fy56.platform.logistics.utils.SessionUtil;
import com.fy56.platform.logistics.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 小程序项目信息controller
 * Created by li on 2018-02-09.
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/controller/miniProgram")
public class MiniProgramController extends BaseController {

    @Autowired
    private IMiniProgramService iMiniProgramService;

    /**
     * 获取项目信息
     *
     * @param keyId
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseBody
    public MiniProgram getMiniProgram(Long keyId, HttpServletRequest request){
        MiniProgram entity = new MiniProgram();
        if(keyId != null){
            entity = iMiniProgramService.selectByPrimaryKey(keyId);
        }
        return entity;
    }

    /**
     * 添加保存
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> save(MiniProgram miniProgram, HttpServletRequest request) {
        ResultModel resultModel = null;
        MiniProgram dbModel = null;
        try {
            if (miniProgram != null) {

                //验证项目是否存在，若项目已存在，提示：该项目已经存在,请重新输入
                dbModel = iMiniProgramService.selectByCode(miniProgram.getTenantCode());
                if (dbModel != null) {
                    return new ResponseEntity<ResultModel>(new ResultModel("该项目已经存在,请重新输入!"), HttpStatus.OK);
                }

                //执行插入
                miniProgram.initInsert(SessionUtil.getSessionUser(request));
                resultModel = iMiniProgramService.insert(miniProgram);

                //保存系统业务日志
                LogUtils.saveLog(request, "成功", miniProgram.getId().toString(), null, "MiniProgramManage", "save", "添加", JSON.toJSONString(miniProgram));
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel(false), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            String key = (miniProgram == null || miniProgram.getId() == null) ? null : miniProgram.getId().toString();
            LogUtils.saveLog(request, "失败", key, null, "MiniProgramManage", "save", "添加", JSON.toJSONString(miniProgram));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            dbModel = null;
        }
    }

    /**
     * 修改保存
     *
     * @param server
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> update(MiniProgram miniProgram, HttpServletRequest request) {
        ResultModel resultModel = null;
        MiniProgram dbModel = null;
        try{
            if (miniProgram != null) {

                //根据主键查询DB中的记录
                dbModel = iMiniProgramService.selectByPrimaryKey(miniProgram.getId());

                //如果DB中的金融代码与页面不等时，提示：金融代码不允许修改！
                if(!dbModel.getTenantCode().equals(miniProgram.getTenantCode())){
                    return new ResponseEntity<ResultModel>(new ResultModel("金融代码不允许修改!"), HttpStatus.OK);
                }else{

                    //设置修改操作员，修改时间（默认当前时间）
                    miniProgram.initUpdate(SessionUtil.getSessionUser(request));

                    //执行更新操作
                    resultModel = iMiniProgramService.update(miniProgram);

                    //保存系统业务日志
                    LogUtils.saveLog(request,"成功",miniProgram.getId().toString(),null,"MiniProgramManage","update","编辑", JSON.toJSONString(miniProgram));
                }
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("项目编辑失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",miniProgram.getId().toString(),null,"MiniProgramManage","update","编辑", JSON.toJSONString(miniProgram));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            dbModel = null;
        }
    }

    /**
     * 删除
     *
     * @param keyId
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> delete(Long keyId, HttpServletRequest request) {
        ResultModel resultModel = null;
        MiniProgram miniProgram = null;
        try{
            if (keyId != null && (keyId.intValue() !=0)) {
                miniProgram = new MiniProgram();
                miniProgram.setId(keyId);
                miniProgram.setDeleteFlag(1);
                miniProgram.initUpdate(SessionUtil.getSessionUser(request));

                //执行删除操作
                resultModel = iMiniProgramService.delete(miniProgram);

                //保存系统业务日志
                LogUtils.saveLog(request,"成功",keyId.toString(),null,"MiniProgramManage","delete","删除", keyId.toString());
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("项目删除失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",keyId.toString(),null,"MiniProgramManage","delete","删除", keyId.toString());
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            miniProgram = null;
        }
    }

}
